package com.swgk.core.customview;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Layout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.swgk.core.R;

/**
 * 展开收起控件
 */
public class SeeMoreView extends FrameLayout {
    //页面控件
    private final TextView tvMore;
    private final TextView tvContent;
    private final TextView tvLine;
    //
    private boolean mIsShowAll = false;//是否展开和收起的标记
    private final int mMaxLine = 3;//最大行数

    private StringBuilder mFoldedTxt = new StringBuilder();//收起的文字
    private StringBuilder mExpandedTxt = new StringBuilder();//展开的文字

    private String mExpText = "展开";
    private String mPackText = "收起";

    public SeeMoreView(@NonNull Context context) {
        this(context, null);
    }

    public SeeMoreView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SeeMoreView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.view_see_more, this);
        tvMore = view.findViewById(R.id.tv_more);
        tvContent = view.findViewById(R.id.tv_content);
        tvLine = view.findViewById(R.id.tv_line);
        //
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SeeMoreView, 0, 0);
        String expText = attributes.getString(R.styleable.SeeMoreView_exp_text);
        String packText = attributes.getString(R.styleable.SeeMoreView_pack_text);
        float size = attributes.getDimension(R.styleable.SeeMoreView_content_size, 0f);
        //获取属性
        if (size != 0f) {
            tvMore.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
            tvLine.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
            tvContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        }
        if (!TextUtils.isEmpty(expText)) {
            mExpText = expText;
        }
        if (!TextUtils.isEmpty(packText)) {
            mPackText = packText;
        }
        attributes.recycle();
        //
        initListener();
    }

    private void initListener() {
        tvMore.setOnClickListener(v -> {
            if (mIsShowAll) {
                //这是收起的关键代码，将最大行数设置为你想展示的最小行数即可
                tvContent.setMaxLines(mMaxLine);
                tvContent.setText(mFoldedTxt);
                tvMore.setText(mExpText);
            } else {
                //这是查看更多的关键代码，将最大行数设置一个大数即可
                tvContent.setMaxLines(Integer.MAX_VALUE);
                tvContent.setText(mExpandedTxt);
                tvMore.setText(mPackText);
            }
            mIsShowAll = !mIsShowAll;
        });
        post(() -> {
            //这里必须这样写，这是在attachedToWindow之后执行操作，否则获取行数会出问题
            if (tvLine.getLineCount() > mMaxLine) {
                calculateContent();
                tvContent.setText(mFoldedTxt);
                tvMore.setVisibility(VISIBLE);
            } else {
                tvContent.setText(tvLine.getText());
                tvMore.setVisibility(GONE);
            }
        });
    }

    /**
     * 计算文字内容
     */
    private void calculateContent() {
        //计算收起时文字和展开时文字
        mExpandedTxt = new StringBuilder(tvLine.getText());
        //
        Layout layout = tvLine.getLayout();
        int lineLength = 0;
        mFoldedTxt = new StringBuilder();
        for (int i = 0; i < mMaxLine; i++) {
            CharSequence lineStr = mExpandedTxt.subSequence(layout.getLineStart(i), layout.getLineEnd(i));
            //记录一排文字长度
            if (i == 0) {
                lineLength = lineStr.length();
            }
            //判断最后一排文字是否超过了一排文字长度的四分之三，是的话裁剪至四分之三
            if (i == mMaxLine - 1 && lineStr.length() >= lineLength * 3 / 4) {
                lineStr = String.format("%s...", lineStr.subSequence(0, lineLength * 3 / 4));
            }
            mFoldedTxt.append(lineStr);
        }
    }

    /**
     * 设置文字
     */
    public void setText(String text) {
        //重新设置监听
        initListener();
        //每次设置文字后都要进行重置
        tvLine.setText(text);
        tvMore.setText(mExpText);
        tvContent.setMaxLines(mMaxLine);
        mIsShowAll = false;
        if (tvLine.getLineCount() > mMaxLine) {
            tvMore.setVisibility(VISIBLE);
        } else {
            tvMore.setVisibility(GONE);
        }
    }
}
